package com.platform.system.controller.role;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.platform.common.annotations.ResponseResult;
import com.platform.common.controller.BaseController;
import com.platform.system.entity.SysRole;
import com.platform.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * describe:角色Api接口
 *
 * @author rainly
 * @date 2019/12/08
 * @time 7:08 下午
 */
@Api(value = "角色controller", tags = {"角色操作接口"})
@RestController
@ResponseResult
@RequestMapping(value = "/system/role")
public class RoleApiController extends BaseController<SysRole> {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 角色列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "角色列表", tags = {"角色列表"}, notes = "必须传入page和limit", httpMethod = "GET")
    @GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object getRoleList(@ApiParam(name = "page", value = "当前分页", required = true) Integer pageNum,
                              @ApiParam(name = "limit", value = "当前分页数量", required = true) Integer pageSize) {
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = this.pageSize;
        }
        PageInfo pageInfo = PageHelper.startPage(pageNum, pageSize, true).doSelectPage(() -> sysRoleService.list()).toPageInfo();
        return pageInfo;
    }

    /**
     * 根据id查询角色
     *
     * @param roleid
     * @return
     */
    @ApiOperation(value = "根据id查询角色", notes = "根据id查询角色", httpMethod = "GET")
    @GetMapping(value = "/{roleid}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object getRoleById(@PathVariable String roleid) {
        return 2;
    }

    /**
     * 修改角色
     *
     * @param sysRole
     * @return
     */
    @ApiOperation(value = "修改角色", notes = "修改角色", httpMethod = "PUT")
    @PutMapping(value = "/{sysRole}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object putRoleById(@ApiParam(name = "角色对象", value = "传入json格式", required = true) @RequestBody SysRole sysRole) {
        return 3;
    }

    /**
     * 修改角色属性
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "修改属性", notes = "修改属性", httpMethod = "PATCH")
    @PatchMapping(value = "/{param}")
    public Object patchRoleByid(@ApiParam(name = "param", value = "传入json格式的角色属性", required = true) @PathVariable String param) {
        return 1;
    }

    /**
     * 新增角色
     *
     * @param sysRole 角色
     * @return 保存结果
     */
    @ApiOperation(value = "新增角色", notes = "新增角色", httpMethod = "POST")
    @PostMapping(value = "/{sysRole}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object postRole(@ApiParam(name = "角色对象", value = "传入json格式", required = true) @RequestBody SysRole sysRole) {
//        SysRole sysRole = JSON.parseObject(role, SysRole.class);
        return sysRole.getRoleId();
    }

    /**
     * 根据id删除角色
     *
     * @param roleid 角色id
     * @return 删除结果
     */
    @ApiOperation(value = "根据id删除角色", notes = "根据id删除角色", httpMethod = "DELETE")
    @DeleteMapping(value = "/{roleid}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object delRoleById(@PathVariable String roleid) {
        return sysRoleService.removeById(roleid);
    }

}
